<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "banner".
 *
 * @property integer $id
 * @property integer $pid
 * @property string $name
 * @property string $link
 * @property string $position_desc
 * @property string $img_url
 * @property integer $width
 * @property integer $height
 * @property string $start_time
 * @property string $end_time
 * @property string $man
 * @property string $email
 * @property string $phone
 * @property string $click_count
 * @property integer $enabled
 * @property integer $orderby
 * @property string $target
 */
class Banner extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'banner';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['pid', 'width', 'height', 'click_count', 'enabled'], 'integer'],
            [['img_url', 'width', 'height',], 'required'],
            [['creat_time', 'end_time'], 'safe'],
            [['orderby'],'unique'],
            [['name', 'link','target'], 'string', 'max' => 60],
            [['position_desc', 'img_url'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'pid' => '广告位置',
            'name' => '广告名称',
            'link' => '广告链接',
            'position_desc' => '广告描述',
            'img_url' => '图片地址',
            'width' => '宽度',
            'height' => '高度',
            'creat_time' => '创建时间',
            'end_time' => '结束时间',
            'click_count' => '点击量',
            'enabled' => '是否显示',
            'orderby' => '排序',
            'target' => '是否新窗口打开',
        ];
    }

     public static function itemAlias ($type, $code = NULL)
    {
        $_items = [
            'pid' => [
                //''  =>'全部',
                '1' => "轮播",
                '3' => "登陆页",            
                '2' => "其他",

            ],
            'enabled' => [
               // ''  =>'是否使用',
                '1' => "显示",              
                '2' => "不显示",
                                
            ],
            'target' => [
               // ''  =>'是否使用',
                '1' => "是",              
                '2' => "否",
                                
            ],
           /* 'orderby' => [
               // ''  =>'是否使用',
                '6' => "等待……",
                '1' => "第一张",              
                '2' => "第二张",
                '3' => "第三张",              
                '4' => "第四张",
                '5' => "第五张",

                                
            ],*/
           
        ];

        if (isset($code))
            return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
        else
            return isset($_items[$type]) ? $_items[$type] : false;
    }

     public static function model($className=__CLASS__){
        return parent::model($className);
    }

     public function beforeSave($insert) {
        if (parent::beforeSave($insert)) {
            //是否是新添加
            if($this->isNewRecord) {
                $this->creat_time = date("Y-m-d H:i:s", time());
                //$this->update_time = date("Y-m-d H:i:s", time());
            }else{
                //$this->update_time = date("Y-m-d H:i:s", time());
            }
            return true;
        } else {
            return false;
        }
    } 
}
